#include<iostream>
using namespace std;
#include<vector>
#include<pthread.h>
#include<unistd.h>
pthread_mutex_t gmutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t gcond = PTHREAD_COND_INITIALIZER;
void *Run(void *args)
{
    string name = static_cast<char *>(args);
    while(1)
    {
        pthread_mutex_lock(&gmutex);
        pthread_cond_wait(&gcond, &gmutex);
        cout << name << endl;
        pthread_mutex_unlock(&gmutex);
        usleep(10000);
    }
}
int main()
{
    int n = 5;
    vector<pthread_t> v;
    for (int i = 0; i < n; i++)
    {
        pthread_t id;
        char *name = new char[128];
        snprintf(name, 128, "thread %d", i + 1);
        pthread_create(&id, nullptr, Run, (void*)name);
        v.emplace_back(id);
    }
    while(1)
    {
        pthread_cond_broadcast(&gcond);//主线程唤醒
        //pthread_cond_signal(&gcond);//主线程唤醒
        cout << "唤醒" << endl;
        usleep(10000);
    }
    for(auto i:v)
    {
        pthread_join(i,nullptr);
    }
}